package com.time.hellotime.db.dao.impl;

import com.time.hellotime.db.dao.BaseDao;
import com.time.hellotime.db.model.UserModel;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Bing on 2016/10/27.
 * E-mail is tx16bing@163.com
 */

public class UserDao extends BaseDao<UserModel> {

    @Override
    public boolean save(UserModel bean) {
        delete(bean);
        return bean.save();
    }

    @Override
    public List<UserModel> find() {
        List<UserModel> all = DataSupport.findAll(UserModel.class);
        if (all == null){
            all = new ArrayList<UserModel>();
        }
        return all;
    }

    @Override
    public int delete(UserModel bean) {
        // 删除缓存数据
        return DataSupport.deleteAll(UserModel.class, "telephone=?", bean.getTelephone());
    }

    public void update(String userId, String nick) {
        List<UserModel> userModels = DataSupport.where("userid=?", userId).find(UserModel.class);
        if (!userModels.isEmpty()){
            UserModel userModel = userModels.get(0);
            userModel.setNickName(nick);
            userModel.save();
        }
    }
}
